/*
 * PWM.cpp
 *
 *  Created on: Jun 8, 2016
 *      Author: Yang
 */

#include "PWM.hpp"

void PWM_Channel_Start(void)
{
    HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_3);
    HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_4);

    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);

}

void PWM_Channel_Stop(void)
{
    HAL_TIM_PWM_Stop(&htim5, TIM_CHANNEL_1);
    HAL_TIM_PWM_Stop(&htim5, TIM_CHANNEL_2);
    HAL_TIM_PWM_Stop(&htim5, TIM_CHANNEL_3);
    HAL_TIM_PWM_Stop(&htim5, TIM_CHANNEL_4);

    HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1);
    HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_2);
    HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_3);
    HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_4);
}

void Single_PWM_Channel_Set(uint8_t Channel, uint16_t Width)
{
    if (Width < 1000)
        Width = 1000;
    else if (Width > 2000)
        Width = 2000;
    switch (Channel)
    {
    case 1:
        TIM5->CCR1 = Width;
        break;
    case 2:
        TIM5->CCR2 = Width;
        break;
    case 3:
        TIM5->CCR3 = Width;
        break;
    case 4:
        TIM5->CCR4 = Width;
        break;

    case 5:
        TIM1->CCR1 = Width;
        break;
    case 6:
        TIM1->CCR2 = Width;
        break;
    case 7:
        TIM1->CCR3 = Width;
        break;
    case 8:
        TIM1->CCR4 = Width;
        break;
    }

}

void PWM_Channel_Set(uint16_t* Width)
{
    uint8_t i = 0;
    for (; i < 8; i++)
    {
        if (Width[i] < 1000)
            Width[i] = 1000;
        else if (Width[i] > 2000)
            Width[i] = 2000;
    }
    TIM5->CCR1 = Width[0];
    TIM5->CCR2 = Width[1];
    TIM5->CCR3 = Width[2];
    TIM5->CCR4 = Width[3];

    TIM1->CCR1 = Width[4];
    TIM1->CCR2 = Width[5];
    TIM1->CCR3 = Width[6];
    TIM1->CCR4 = Width[7];
}

